<?php
class ui_input_select extends ui_input
{
	function __construct($name,$value=null,$value_column=null,$text_column=null)
	{
		$this->init(__FILE__,'select');
		$this->set('name',$name);
		$this->set('value',$value);
		$this->text_column  = (is_null($text_column))?'name':$text_column;
		$this->value_column = (is_null($value_column))?$name:$value_column;
		$this->source = null;
	}
	
	function pre_render($data)
	{
		global $ti;
		parent::pre_render($data);
		if(is_array($data) && $this->check_data_value)
		{
			$this->set('value',$data[$this->name]);
		}
		
		$selected_value = $this->get('value');
		#$ti->log('rendering select '.$this->name.': '.$selected_value);
		
		#$ti->log('type is: '.gettype($this->source));
		if(is_array($this->source))
		{
			foreach($this->source as $index=>$label)
			{
				$this->add(
					new ui_input_option(
						$label.'',
						$index.'',
						($index == $selected_value)
					)
				);
			}
		}
		
		# if it's not an array, we could also use any 
		# object that supports a ->Next iterator
		if(method_exists($this->source,'next'))
		{
			#$ti->log('found an object type data source for the select list');			
			while($this->source->next())
			{
				#$ti->log('data:'.print_r($this->source->data,true));
				#;
				#if('loc_id_1' == $this->name)
				#		$ti->log('got data: '.$this->source->data[$this->value_column]);
				#.'/'.$selected_value.'/'.($this->source->data[$this->value_column] == $selected_value));
				$this->add(
					new ui_input_option(
						$this->source->data[$this->text_column],
						$this->source->data[$this->value_column],
						($this->source->data[$this->value_column] == $selected_value)
					)
				);				
			}
		}
	}
}
?>